package com.magic.gre.ui.dialog;

import android.os.Bundle;
import android.support.v4.app.FragmentManager;
import android.support.v4.content.ContextCompat;
import android.view.View;

import com.magic.gre.R;
import com.magic.gre.adapter.WheelAdapter;
import com.magic.gre.base.dialog.BaseBottomDialog;
import com.magic.gre.entity.base.BaseWheelIm;
import com.magic.gre.widget.wheel.WheelView;

import java.util.ArrayList;
import java.util.List;

/**
 * Author: Lmoumou
 * Date: 2018-12-20 11:45
 * 文件名: MultipleBottomDialog
 * 描述:
 */
public class MultipleBottomDialog<T extends BaseWheelIm> extends BaseBottomDialog {

    private WheelView<T> mWheelView;

    private OnSelectListener onSelectListener;

    private int selectPosition = 0;

    private List<T> dataList = new ArrayList<>();

    public void setOnSelectListener(OnSelectListener onSelectListener) {
        this.onSelectListener = onSelectListener;
    }


    public void showThis(FragmentManager manager, String tag, List<T> dataList, int position) {
        super.showThis(manager, tag);
        this.dataList.clear();
        this.dataList.addAll(dataList);
        selectPosition = position;
    }

    @Override
    protected void initView(View view) {
        mWheelView = view.findViewById(R.id.mWheelview);
        mWheelView.setWheelAdapter(new WheelAdapter<T>(getContext()));
        mWheelView.setSkin(WheelView.Skin.Holo);
        mWheelView.setWheelData(dataList);
        mWheelView.setWheelSize(5);
        WheelView.WheelViewStyle style = new WheelView.WheelViewStyle();
        style.selectedTextSize = 15;
        style.textSize = 15;
        style.backgroundColor= ContextCompat.getColor(mContext,R.color.window_bg);
        style.holoBorderColor =ContextCompat.getColor(mContext,R.color.color_DDDDDD);
        style.textColor = ContextCompat.getColor(mContext,R.color.color_999999);
        style.selectedTextColor = ContextCompat.getColor(mContext,R.color.main_color);
        mWheelView.setStyle(style);
        mWheelView.setSelection(selectPosition);

        view.findViewById(R.id.sure_tv).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (onSelectListener != null) {
                    T wheelBeen = mWheelView.getSelectionItem();
                    onSelectListener.onSetect(wheelBeen, getTag(), mWheelView.getCurrentPosition());
                    MultipleBottomDialog.this.dismissThis(isResumed());
                }


            }
        });

        view.findViewById(R.id.cancle_tv).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                mWheelView.setSelection(0);

            }
        });

    }

    @Override
    protected int setViewId() {
        return R.layout.dialog_multiple_bottom;
    }

    @Override
    protected void initBundle(Bundle bundle) {

    }

    public interface OnSelectListener<T> {
        void onSetect(T selectBean, String tag, int position);
    }
}
